package xhwl.pulltorefresh;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;

/**
 * Created by admin on 2017-11-08.
 */

public class DefaultHeader extends LinearLayout implements RefreshHeader {
    ImageView ivHeader;
//    GifImageView gifView;
    AnimationDrawable animationDrawable;
    int  height;
    SpinnerStyle style=SpinnerStyle.FixedBehind;
    public DefaultHeader(Context context) {
        this(context,null);
    }

    public DefaultHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.header_default,this);
//        gifView= (GifImageView) findViewById(R.id.gifView);
        ivHeader= (ImageView) findViewById(R.id.ivHeader);
        ivHeader.setBackgroundResource(R.drawable.pull_down);
        animationDrawable=(AnimationDrawable)ivHeader.getBackground();

        LinearLayout llHeader= (LinearLayout) findViewById(R.id.llHeader);
        llHeader.measure(0,0);
        //获取组件的高度  
        height=llHeader.getMeasuredHeight();
    }
    public int getHeaderHeight(){
        return height;
    }
    @Override
    public void onPullingDown(float percent, int offset, int headerHeight, int extendHeight) {

    }

    @Override
    public void onReleasing(float percent, int offset, int headerHeight, int extendHeight) {

    }

    protected void setSpinnerStyle(SpinnerStyle style){
        this.style=style;
    }
    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return style;
    }

    @Override
    public void setPrimaryColors(@ColorInt int... colors) {

    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int height, int extendHeight) {
        if(!animationDrawable.isRunning()){
            animationDrawable.start();
        }
    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        if(animationDrawable.isRunning()){
            animationDrawable.stop();
        }
        return 200;
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {

    }
}
